package vn.info.vietnaminfo.utli;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.info.vietnaminfo.data.DSDiaDiem;
import vn.info.vietnaminfo.data.DSLoaiHinhKD;
import android.content.Context;
import android.util.Log;

public class JSONUtil
{
	// Get danh sach loai hinh kinh doanh

	
	public static List<DSLoaiHinhKD> getDSLoaiHinhKD(Context mContext,String url)
	{
		try
		{
			List<DSLoaiHinhKD> list = new ArrayList<DSLoaiHinhKD>();
			
			String ma_loai_kd = "", ten_loai_kd = "";
			byte[] array = HttpUtil.httpGetByte(url, 180, 100);
			String json  = new String(array);
			
			ShareDefaultData.setUserDefaultData(mContext, ShareDefaultData.KEY_DS_LOAI_HINH_KINH_DOANH, json);
			
			Log.v("JsonUtli","HungCX---> json = "+json);
			JSONArray jsonArray = new JSONArray(json);
			int json_size = jsonArray.length();
			Log.v("JsonUtli","HungCX---> json_size = "+json_size);
			
			
			for(int i = 0; i< json_size;i++)
			{
				ma_loai_kd 	= jsonArray.getJSONObject(i).getString("MA_LOAI_KD").toString();
				ten_loai_kd = jsonArray.getJSONObject(i).getString("TEN_LOAI_KD").toString();
				list.add(new DSLoaiHinhKD(ma_loai_kd, ten_loai_kd));
			}
			return list;
					
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}
	
	public static List<DSLoaiHinhKD> getDSLoaiHinhKDNotConnect(Context mContext,String json)
	{
		try
		{
			List<DSLoaiHinhKD> list = new ArrayList<DSLoaiHinhKD>();
			
			String ma_loai_kd = "", ten_loai_kd = "";
			
			Log.v("JsonUtli","HungCX---> json = "+json);
			JSONArray jsonArray = new JSONArray(json);
			int json_size = jsonArray.length();
			Log.v("JsonUtli","HungCX---> json_size = "+json_size);
			
			
			for(int i = 0; i< json_size;i++)
			{
				ma_loai_kd 	= jsonArray.getJSONObject(i).getString("MA_LOAI_KD").toString();
				ten_loai_kd = jsonArray.getJSONObject(i).getString("TEN_LOAI_KD").toString();
				list.add(new DSLoaiHinhKD(ma_loai_kd, ten_loai_kd));
			}
			return list;
					
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}
	
	public static List<DSDiaDiem> getListRetailerShop(Context mContext, String url)
	{
		try
		{
			List<DSDiaDiem> listRetailer = new ArrayList<DSDiaDiem>();
			
			String ma_dia_diem;
			String ten_dia_diem;
			String loai_hinh_kinh_doanh;
			String dia_chi;
			String phone;
			String fax;
			String mo_ta;
			String tieu_de;
			String website;
			String url_image;
			double latitude;
			double longitude;
			String reg_upd_dt;
			boolean title;
			boolean open_now;
			byte[] array = HttpUtil.httpGetByte(url, 180, 100);
			String json  = new String(array);
			JSONObject jsonObject = new JSONObject(json);
			Log.v("JsonUtli","HungCX---> jsonObject = "+jsonObject);
			int json_size = ((JSONArray)jsonObject.get("results")).length();
			Log.v("JsonUtli","HungCX---> json_size = "+json_size);
			
			JSONArray jsonArray = (JSONArray)jsonObject.get("results");
			
			
			for(int i = 0; i< json_size;i++)
			{
				ma_dia_diem = jsonArray.getJSONObject(i).getString("id");
				Log.v("JsonUtli","HungCX---> ma_dia_diem = "+ma_dia_diem);
				
				ten_dia_diem = jsonArray.getJSONObject(i).getString("name");
				Log.v("JsonUtli","HungCX---> ten_dia_diem = "+ten_dia_diem);
				
				loai_hinh_kinh_doanh = "chưa có";
				Log.v("JsonUtli","HungCX---> ten_dia_diem = "+ten_dia_diem);
				
				dia_chi = jsonArray.getJSONObject(i).getString("vicinity");
				Log.v("JsonUtli","HungCX---> dia_chi = "+dia_chi);
				
				phone 	= "0975979690";
				Log.v("JsonUtli","HungCX---> phone = "+phone);
				
				fax 	= "0975979690 - fax";
				Log.v("JsonUtli","HungCX---> fax = "+fax);
				
				mo_ta = "chưa có mô tả";
				Log.v("JsonUtli","HungCX---> mo_ta = "+mo_ta);
				
				tieu_de = "chưa có tiêu đề";
				Log.v("JsonUtli","HungCX---> tieu_de = "+tieu_de);
				
				website = "https://www.google.com.vn/";
				Log.v("JsonUtli","HungCX---> website = "+website);
				
				url_image = jsonArray.getJSONObject(i).getString("icon");
				Log.v("JsonUtli","HungCX---> url_image = "+url_image);
				
				longitude 		= jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
				Log.v("JsonUtli","HungCX---> longitude = "+longitude);
				
				latitude 		= jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONObject("location").getDouble("lat");
				Log.v("JsonUtli","HungCX---> url_image = "+url_image);
				
				reg_upd_dt = "chưa có update date";
				
				open_now = true;//jsonArray.getJSONObject(i).getBoolean("open_now");
				Log.v("JsonUtli","HungCX---> open_now = "+open_now);
				
				
				
				listRetailer.add(new DSDiaDiem(ma_dia_diem, ten_dia_diem, loai_hinh_kinh_doanh, dia_chi, phone, fax, mo_ta, tieu_de, website, url_image, latitude, longitude, reg_upd_dt, false, open_now));
			}
			return listRetailer;
					
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		return null;
	}
	
	
	
	// Get danh sÃ¡nh cá»­a hÃ ng
//	public static List<DSDiaDiem> getDSCuaHang(String url)
//	{
//		try
//		{
//			List<DSDiaDiem> list = new ArrayList<DSDiaDiem>();
//			String jsontext = HttpUtil.httpGetString(url);
//			// TrÆ°á»?ng há»£p get json text khÃ´ng xáº£y ra lá»—i
//			if(!jsontext.equals("Error"))
//			{
//				JSONArray array = new JSONArray(jsontext);			
//				for(int i = 0; i < array.length(); i++)
//				{
//					/*list.add(new DSCuaHang(array.getJSONObject(i).getString("MA_CUA_HANG").toString(),
//							array.getJSONObject(i).getString("TEN_CUA_HANG").toString(), "",
//							array.getJSONObject(i).getString("LOAI_CUA_HANG").toString(),
//							array.getJSONObject(i).getString("DIA_CHI").toString(),
//							array.getJSONObject(i).getString("QUAN").toString(),
//							array.getJSONObject(i).getString("HUYEN").toString(),
//							array.getJSONObject(i).getString("PHUONG").toString(),
//							array.getJSONObject(i).getString("THANH_PHO").toString(),
//							array.getJSONObject(i).getString("PHONE_NUMBER").toString(),
//							array.getJSONObject(i).getString("FAX").toString(),
//							array.getJSONObject(i).getString("MO_TA_LOAI_HINH_KD").toString(),
//							array.getJSONObject(i).getString("WEBSITE").toString(),
//							array.getJSONObject(i).getString("URL_IMAGE").toString(),
//							array.getJSONObject(i).getString("LAT").toString(),
//							array.getJSONObject(i).getString("LONG").toString()));*/
//				}
//				return list;
//			}
//		}
//		catch (Exception e)
//		{
//			e.printStackTrace();
//		}
//		return null;
//	}
}